//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖保佑             永无BUG 
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//                  不见满街漂亮妹，哪个归得程序员？  


/* ****************************************************************************
 * Author:      671643387
 * Date:        2015/9/22
 * Description: 英雄志
 * ***************************************************************************/

/**
 * 获取英雄志数据
 * @param player
 * @param req
 * @param resp
 * @param onHandled
 */
exports.get = function(player, req, resp, onHandled) {
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var heroLog = gHeroLog.getByUid(player);
        resp.data = {
            dragonball: heroLog.dragonball,
            placement_specialAward: heroLog.placement_specialAward,
            placement_num: heroLog.placement_num,
            placement_buy_num: heroLog.placement_buy_num,
            placement_heros: heroLog.placement_heros,
            chapters: heroLog.chapters
        };
    } while (false);

    onHandled && onHandled();
};

/**
 * 闯关战斗
 * @param player
 * @param req
 * @param resp
 * @param onHandled
 */
exports.battle = function(player, req, resp, onHandled){
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var chapId = +req.args.chapId;
        var chkptId = +req.args.chkptId;
        if (isNaN(chapId) || isNaN(chkptId) || chkptId > 8) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        if (!gConfHeroLog[chapId]) {
            resp.code = 1; resp.desc = 'this chapter is not configured'; break;
        }

        if (chapId > +gConfGlobal.HeroChapterMaxId) {
            resp.code = 1; resp.desc = 'limit max open'; break;
        }

        gHeroLog.battle(player, chapId, chkptId, function(err, res){
            if (err) {
                resp.code = 1;
                resp.desc = err;
            } else {
                resp.code = 0;
                resp.data = res;
            }
            onHandled && onHandled();
        });
        return;
    } while (false);
    onHandled && onHandled();
};

/**
 * 播放章节动画
 * @param player
 * @param req
 * @param resp
 * @param onHandled
 */
exports.play_chapter_animation = function(player, req, resp, onHandled){
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var chapId = +req.args.chapId;
        if (isNaN(chapId)) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }
        if (!gConfHeroLog[chapId]) {
            resp.code = 1; resp.desc = 'this chapter is not configured'; break;
        }
        if (chapId > +gConfGlobal.HeroChapterMaxId) {
            resp.code = 1; resp.desc = 'limit max open'; break;
        }
        gHeroLog.play_chapter_animation(player, chapId, function(err, res){
            if (err) {
                resp.code = 1;
                resp.desc = err;
            } else {
                resp.code = 0;
                resp.data = res;
            }
            onHandled && onHandled();
        });
        return;
    } while (false);
    onHandled && onHandled();
};

/**
 * 放置玩法派遣武将
 * @param player
 * @param req
 * @param resp
 * @param onHandled
 */
exports.placement = function(player, req, resp, onHandled){
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var chapId = +req.args.chapId;
        var mainRole = +req.args.mainRole;
        var rRole1 = +req.args.rRole1;
        var rRole2 = +req.args.rRole2;
        if (isNaN(chapId) || isNaN(rRole1) || isNaN(rRole2) || isNaN(mainRole)) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }
        if (chapId > +gConfGlobal.HeroChapterMaxId) {
            resp.code = 1; resp.desc = 'limit max open'; break;
        }

        if (mainRole == 0 && rRole1 == 0 && rRole2 == 0) {
            resp.code = 1; resp.desc = 'placement 0, 0, 0 ?'; break;
        }

        if (!gConfHeroLog[chapId]) {
            resp.code = 1; resp.desc = 'this chapter is not configured'; break;
        }
        if ((mainRole == rRole1 && mainRole != 0)
            || (mainRole == rRole2 && mainRole != 0)
            || (rRole1 == rRole2 && rRole1 != 0)) {
            resp.code = 1; resp.desc = 'the three roles must 不同.'; break;
        }
        gHeroLog.placement(player, chapId, mainRole, rRole1, rRole2, function(err, res){
            if (err) {
                resp.code = 1;
                resp.desc = err;
            } else {
                resp.code = 0;
                resp.data = res;
            }
            onHandled && onHandled();
        });
        return;
    } while (false);
    onHandled && onHandled();
};

exports.buy = function(player, req, resp, onHandled){
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        gHeroLog.buy(player, function(err, res){
            if (err) {
                resp.code = 1;
                resp.desc = err;
            } else {
                resp.code = 0;
                resp.data = res;
            }
            onHandled && onHandled();
        });
        return;
    } while (false);
    onHandled && onHandled();
};

exports.chapter_award = function(player, req, resp, onHandled){
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var chapId = +req.args.chapId;
        if (isNaN(chapId)) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }
        if (chapId > +gConfGlobal.HeroChapterMaxId) {
            resp.code = 1; resp.desc = 'limit max open'; break;
        }
        gHeroLog.chapter_award(player, chapId, function(err, res){
            if (err) {
                resp.code = 1;
                resp.desc = err;
            } else {
                resp.code = 0;
                resp.data = res;
            }
            onHandled && onHandled();
        });
        return;
    } while (false);
    onHandled && onHandled();
};

exports.skip = function(player, req, resp, onHandled){
    do {
        if (player.user.status.level < +gConfGlobal.HeroLogOpenLevel) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        if (!config.AllowSkipHeroLog) {
            resp.code = 1; resp.desc = 'not allow'; break;
        }

        var chapId = +req.args.chapId;
        if (isNaN(chapId)) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }
        if (chapId > +gConfGlobal.HeroChapterMaxId) {
            resp.code = 1; resp.desc = 'limit max open'; break;
        }
        gHeroLog.skip(player, chapId);
    } while (false);
    onHandled && onHandled();
};